<?php
class userActions extends BaseUserActions{	
	public function __construct(){
		parent::__construct();
		$this->assign('keywords', '');
		$this->assign('description', '');
		$this->assign('title', '');
	}
	public function actionsIndex(){
		
	}
	public function actionsLogin(){	    
	    $ischeckcode = Files::getConfig('setting.inc', 'authcode', 'authlogin');
		if (Http::post()){
			
		    $username = Http::post('username');
		    $pwd = Http::post('pwd');
		    $rs = '登录失败';
		    $info='';
		    if ($ischeckcode){
    		    $checkcode = Http::post('checkcode');		
    			if (strtolower($checkcode)!=strtolower(@$_SESSION['captcha'])){
    				$rs = '验证码错误！';
    			}
		    }
		    $email = filter_input(INPUT_POST,'username',FILTER_VALIDATE_EMAIL);
		    
		    if ($email){		    	
		    	$info = $this->m->getBasicUserByEmail($email);
		    	if (!$info){
		    		$rs = '帐号或密码错误';
		    	}
		    }else {
		    	if (checkPhone($username) and strlen($username)==11){
		    		$info = $this->m->getBasicUserByPhone($username);
		    		if (!$info){
		    			$rs = '帐号或密码错误';
		    		}
		    	}else {
		    		$rs = '帐号格式错误';
		    	}
		    }
		    //print_r($info);   
		    if ($info){
		    	if ($info['pwd']!=md5($pwd.'z')){
		    		$rs = '帐号或密码错误';
		    	}else {
		    		$this->m->setLogoin($info['uid']);
		    		die('成功');
		    	}
		    }
		    die($rs);
		}	
	    if (preg_match('/user\//', MVC_PRE_PAGE)){
		    $loginurl = MVC_PRE_PAGE;
	    }else {
	        $loginurl = MVC_WWW_HOST.'user';
	    }
		$this->assign('ischeckcode', $ischeckcode);		
		$this->assign('tourl', $loginurl);
	}
	public function actionsReg(){
		$ischeckcode = Files::getConfig('setting.inc', 'authcode', 'authreg');
		if (Http::post()){
			$username = Http::post('username');			
			$pwd = Http::post('pwd');
			$phone = Http::post('phone');
			$name = Http::post('name');
			$code = Http::post('code');
			$rs = '注册失败';
			$email=Http::post('email');
			if ($email){
				$email = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL);
				if (!$email){
					die('邮箱格式错误！');
				}
			}
						
			if ($phone){
    			if (!checkPhone($phone)){
    				die('请填写正确的手机号！');
    			}
			}
			if (!$email and !$phone){
				die('请选择邮箱或手机进行注册！');
			}
			if (strlen($pwd)>16 or strlen($pwd)<6){
				die('密码长度需大于6位小于16位！');
			}						
			if ( strlen($username)>16 or strlen($username)<3){
			    die('用户名长度需大于3位小于16位');
			}
		    if ( strlen($name)>16 or strlen($name)<6){
			    die('姓名长度需大于2位小于16位');
			}
			$ccode = idcard_checksum18($code);
			if ($ccode!==true){
				die($ccode);
			}
			if ($ischeckcode){
				$checkcode = Http::post('checkcode');
				if (strtolower($checkcode)!=strtolower(@$_SESSION['captcha'])){
					die('验证码错误！');
				}
			}
			
			if ($this->m->getBasicUserByEmail($email)){
			    die('您的邮箱已经注册');
			}
		    if ($this->m->getBasicUserByUsername($username)){
			    die('您的昵称已经注册');
			}
			if ($this->m->getBasicUserByCode($code)){
				die('您的身份证已经注册');
			}
			$data = array('username'=>$username,'email'=>$email,'phone'=>$phone,'pwd'=>md5($pwd.'z'),'regtime'=>date("Y-m-d H:i:s"),'name'=>$name,'code'=>$code);
			$uid = $this->m->insertUser($data);
			if($uid){
				$this->m->setLogoin($uid);
				die('成功');
			}	    
			die($rs);
		}
		$this->assign('ischeckcode', $ischeckcode);
		$this->assign('tourl', MVC_WWW_HOST.'user');
	}
	public function actionsLogout(){
		session_destroy();
		if (MVC_PRE_PAGE){
			$this->tourl(MVC_PRE_PAGE);
		}else {
			$this->tourl(MVC_WWW_HOST);
		}
	}
	public function actionsBinfo(){
	    $jsoncallback = Http::get('jsoncallback');
	    $data = array('result'=>0);
	    if (key_exists('islogin', $_SESSION) and $jsoncallback){
	        $uid = $_SESSION['user']['uid'];
	        $data = $this->m->getBasicUserByUid($uid);
	        $data['result']=1;
	    }
	    
	    die($jsoncallback.'('.json_encode($data).')');
	}
	public function actionsSaveuserinfo(){
	    if (Http::post()){
			$username = Http::post('username');			
			$pwd = Http::post('pwd');
			$phone = Http::post('phone');
			$name = Http::post('name');
			$code = Http::post('code');
			$rs = '注册失败';
			$email=Http::post('email');
			if ($email){
				$email = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL);
				if (!$email){
					die('邮箱格式错误！');
				}
			}
						
			if ($phone){
    			if (!checkPhone($phone)){
    				die('请填写正确的手机号！');
    			}
			}
			if (!$email and !$phone){
				die('请选择邮箱或手机进行注册！');
			}
			if ($pwd){
    			if (strlen($pwd)>16 or strlen($pwd)<6){
    				die('密码长度需大于6位小于16位！');
    			}
    			$pwd = md5($pwd.'z');	
			}else {
			    $pwd = $_SESSION['user']['pwd'];
			}
			if ( strlen($username)>16 or strlen($username)<3){
			    die('用户名长度需大于3位小于16位');
			}
		    if ( strlen($name)>16 or strlen($name)<6){
			    die('姓名长度需大于2位小于16位');
			}
			$ccode = idcard_checksum18($code);
			if ($ccode!==true){
				die($ccode);
			}
	        if ($phone and !$_SESSION['user']['phone']){
    			if ($this->m->getBasicUserByPhone($phone)){
    			    die('您的邮箱已经注册');
    			}
			}		
			if ($email and !$_SESSION['user']['email']){
    			if ($this->m->getBasicUserByEmail($email)){
    			    die('您的邮箱已经注册');
    			}
			}
			if ($username and $username!=$_SESSION['user']['username']){
    		    if ($this->m->getBasicUserByUsername($username)){
    			    die('您的昵称已经注册');
    			}
			}			
			if ($code and $code!=$_SESSION['user']['code']){
    			if ($this->m->getBasicUserByCode($code)){
    				die('您的身份证已经注册');
    			}
			}
			$data = array('username'=>$username,'email'=>$email,'phone'=>$phone,'pwd'=>$pwd,'name'=>$name,'code'=>$code);
			$uid = $this->m->saveBasicinfo($data,$this->uid);
			if($uid){
				$this->m->setLogoin($this->uid);
				die('成功');
			}	    
			die($rs);
	    }
	}
	public function actionsAddress(){
		$uid = $this->uid;
	    if (Http::get('do')=='save'){
	        $name = Http::post('name');
	        $phone = Http::post('phone');
	        $tel1 = Http::post('tel1');
	        $tel2 = Http::post('tel2');
	        $tel3 = Http::post('tel3');
	        $areacode = Http::post('areacode');
	        $province = Http::post('province');
	        $city = Http::post('city');
	        $area = Http::post('area');
	        $street = Http::post('street');
	        $isdefault = Http::post('isdefault');
	        if (!$name or !$areacode or !$province or !$city or !$area or !$street ){
	        	die('填写信息不能为空！');
	        }
	        if ($tel1 and !$tel2){
	        	die('填写信息不能为空！');
	        }
	        if (!$phone and !$tel1){
	        	die('填写信息不能为空！');
	        }
	        if ($phone){
	        	if (!checkPhone($phone)){
	        		die('请正确填写手机号！');
	        	}
	        }
	        
	        if ($isdefault=='true'){ 
	        	$isdefault=1;
	        }else {
	        	$isdefault=0;
	        }
	        if ($this->m->getAddressCount($uid)>=5){
	        	die('最多能设置5个地址！');
	        }
	        
	        $data = array('uid'=>$uid,'name'=>$name,'phone'=>$phone,'tel1'=>$tel1,'tel2'=>$tel2,'tel3'=>$tel3,
	        'areacode'=>$areacode,'province'=>$province,'city'=>$city,'area'=>$area,'street'=>$street,'isdefault'=>$isdefault);
	        $id = $this->m->db->insert('user_addr', $data,true);
	        if ($isdefault) $this->actionsSetdeaultaddr($id);
	        echo '添加成功！';
	        die();
	    }
	    $this->addJs(MVC_WWW_HOST.'js/system/jsAddress.js');
	    $rs = $this->m->getAddress($this->uid);
	    $this->assign('rs', $rs);
	    
	    
	}
	public function actionsSetdeaultaddr($id=null){
		$uid = $this->uid;		
		$aid=Http::get('id',$id);
		if (!$aid){
			die('请设置id');
		}
		$data = array('isdefault'=>0);
		$this->m->db->updateData('user_addr', $data, " WHERE uid = '$uid'");
		$data = array('isdefault'=>1);
		$this->m->db->updateData('user_addr', $data, " WHERE id='$aid' AND uid = '$uid'");
		if (!$id) {
			die('设置成功');
		}else {
			return true;
		}
	}
	public function actionsBank(){
		$uid = $this->uid;
		$row=array();
	    if(Http::get('do')=='save'){
	        $id = Http::get('id');
	    	$name = Http::post('name');
	    	$bankname = Http::post('bankname');
	    	$branch = Http::post('branch');
	    	$bankno = Http::post('bankno');
	    	$phone = Http::post('phone');
	    	$province = '';
	        $city = '';
	        $area = '';
	    	if ($bankname!='支付宝'){	    	
    	    	$province = Http::post('province');
    	        $city = Http::post('city');
    	        $area = Http::post('area');
	    	}
	    	$isdefault = Http::post('isdefault');
	        if (!$name or !$bankname or !$bankno  ){
	        	die('填写信息不能为空！');
	        } 
	        if ($isdefault=='true'){ 
	        	$isdefault=1;
	        }else {
	        	$isdefault=0;
	        }
	        if ($this->m->getBankCount($uid)>=5){
	        	die('最多能设置5个收款信息！');
	        }	        
	        $data = array('uid'=>$uid,'name'=>$name,'province'=>$province,'city'=>$city,'area'=>$area,
	        	        'bankname'=>$bankname,'branch'=>$branch,'bankno'=>$bankno,'phone'=>$phone,'isdefault'=>$isdefault);
	        if ($id){
	            $this->m->db->updateData('user_bank', $data," WHERE id='$id' AND uid = '$uid'");
	        }else{
	            $id = $this->m->db->insert('user_bank', $data,true);
	        }
	        if ($isdefault) $this->actionsSetdeaultBank($id);
	        echo '保存成功！';
	        die();
	    }elseif (Http::get('do')=='edit'){
	        $id = Http::get('id');
	        $sql = "SELECT * FROM user_bank WHERE id='$id' AND uid = '$uid' ";
	        $row = $this->m->db->getRow($sql);
	        
	    }
	    $this->assign('row', $row);
	    $this->addJs(MVC_WWW_HOST.'js/system/jsAddress.js');
	    $rs = $this->m->getBank($this->uid);
	    $this->assign('rs', $rs);
	}
	public function actionsSetdeaultBank($id=null){
		$uid = $this->uid;		
		$aid=Http::get('id',$id);
		if (!$aid){
			die('请设置id');
		}
		$data = array('isdefault'=>0);
		$this->m->db->updateData('user_bank', $data, " WHERE uid = '$uid'");
		$data = array('isdefault'=>1);
		$this->m->db->updateData('user_bank', $data, " WHERE id='$aid' AND uid = '$uid'");
		if (!$id) {
			die('设置成功');
		}else {
			return true;
		}
	}
	public function actionsPublish(){
	    if (Http::get('do')=='save'){
	    	$id = Http::get('id');
	    	$type = Http::post('type');
	    	$typeinfo = Http::post('typeinfo');
	    	$isfree = Http::post('isfree');
	    	$title = Http::post('title');
	    	$content = Http::post('content');
	    	$desc = Http::post('desc');
	    	$img = Http::post('img');
	    	$point = Http::post('point');
	    	
	    	$t = time();
	    	
		    if($type!='0' && $type!=4){
				if($typeinfo==''){
					echo '正确填写信息！';
					die();					
				}
			}
			if($point=='' && $isfree=='0'){
				echo '您选择了共享信息，请设置积分！';
				die();
			}
			if(!$title){
				echo '标题必须填写！';
				die();
			}
			if(!$desc){
				echo '简介必须填写！';
				die();
			}
			if(!$content){
				echo '详细内容！';
				die();
			}
	    	
	    	$data = array('uid'=>$this->uid,'type'=>$type,'typeinfo'=>$typeinfo,'isfree'=>$isfree,'title'=>$title,'content'=>$content,'desc'=>$desc,'img'=>$img,'point'=>$point,'ctime'=>$t,'utime'=>$t);
	    	if ($id){
	    		unset($data['ctime']);	    		
	    		if($this->m->db->updateData('user_publish', $data, " WHERE id='$id' AND uid = '".$this->uid."'")){
	    			echo '修改成功';
	    			die();
	    		}
	    	}else {
		    	if($this->m->db->insert('user_publish', $data)){
		    		echo '添加成功';
		    		die();
		    	}
	    	}
	    	echo '提交失败';
	    	die();
	    }elseif (Http::get('do')=='list'){
	        $rs = $this->m->getUserPublish($this->uid);
	        $this->assign('rs', $rs);
	        $this->display('publish_list.tpl.php');
	    }elseif (Http::get('do')=='edit'){
	    	
	        $rs = $this->m->getUserPublishInfo(Http::get('id'),$this->uid);
	        //print_r($rs);
	        $this->assign('info', $rs);
	    }
	}
    public function actionsLottery(){
    	$ar = array(
    			1=>1,
    			2=>4,
    			3=>6,
    			4=>10,
    			5=>15,
    			6=>20,
    			7=>50,
    			8=>100);
    	$ar2 = array(
    			1=>'1分',
    			2=>'4分',
    			3=>'6分',
    			4=>'10分',
    			5=>'15分',
    			6=>'20分',
    			7=>'50分',
    			8=>'100分');
	    if(Http::get('do')=='award'){
	    	
	    	$arr = array(
	    			1=>6,//1分
	    			2=>24,//4分
	    			3=>30.99,//6分
	    			4=>10,//10分
	    			5=>25,//15分
	    			6=>3,//20分
	    			7=>1,//50分
	    			8=>0.01//100分
	    			);	
	    	 
	    	$v = pro_rand($arr);
	    	
	    	$p = $ar[$v];
	    	 
	    	$msg='';
	    	if ($p>'0') $msg='再接再厉！';
	    	if ($p>='10') $msg='你真棒！';
	    	if ($p>='20') $msg='你太牛了！';
	    	if ($p=='50') $msg='真是神了！';
	    	if ($p=='100') $msg='逆天了！';
	    	$msg2 = $msg.'你获得了：'.$p.'分！';
	    	$msg = date("H:i:s ").'你获得了：<span class="point">'.$p.'</span>分！';
	    	if (!$this->m->setUserData(array('point'=>-10))){
	    		echo json_encode(array('result'=>0,'msg'=>'你的积分不足，快去赚积分吧'));
	    		die();
	    	}
	    	$data = array('uid'=>$this->uid,'type'=>5,'point'=>$p,'expend'=>10,'timeline'=>time(),'remark'=>'幸运转盘');
	    	$this->m->db->insert('user_award', $data);
	    	
	    	echo json_encode(array('result'=>1,'num'=>$v,'msg'=>$msg,'msg2'=>$msg2));	 
	    	die();
	    }elseif (Http::get('do')=='awardlist'){
	    	echo json_encode(array('result'=>1,'msg'=>'成功','list'=>$ar2));
	    	die();
	    }elseif (Http::get('do')=='24dian'){
	    	$this->display('lottery_24.tpl.php');
	    }elseif (Http::get('do')=='list'){
	    	$rs = $this->m->getLottery($this->uid);
	    	$this->assign('rs', $rs);	
	    	$this->display('lottery_list.tpl.php');
	    }
	    $this->addJs(MVC_WWW_HOST.'js/jquery/swfobject.js');
	    
	}
	public function actionsExchange(){
	    if (Http::get('do')=='cash'){
	        $this->display('exchange_cash.tpl.php');
	    }
	}
	public function actionsEarn(){
	    
	}
	public function actionsBasicinfo(){
	    
	}
	public function actionsChecklogin(){
		echo json_encode(array('result'=>1,'msg'=>'成功'));
		die();
	}
}

